package com.ants.utils;

import com.ants.model.ColumnModel;
import com.ants.model.TableModel;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author 蚂蚁会花呗
 * @date 2021/11/3 13:48
 */
public class ExportUtils {

    /**
     *
     * @param tableModels
     * @param filePath
     */
    public static void export(List<TableModel> tableModels, String filePath) throws Exception{
        //创建HSSFWorkbook对象
        HSSFWorkbook wb = new HSSFWorkbook();

        CellStyle cellStyle1 = createTitleCellStyle(wb);

        for(TableModel table : tableModels){
            //创建HSSFSheet对象
            HSSFSheet sheet = wb.createSheet(table.getTableName());

            sheet.setDefaultColumnWidth(20);

            int rowNum = 0;
            HSSFRow row = sheet.createRow(rowNum++);
            //创建HSSFCell对象
            for (int i=0;i<5;i++){
                HSSFCell cell=row.createCell(i);
                if (i==0){
                    cell.setCellValue("表名");
                }else if(i==1){
                    cell.setCellValue(table.getTableName());
                }else if(i==2){
                    cell.setCellValue("表名称");
                }else if(i==3){
                    cell.setCellValue(table.getTableDesc());
                }else{
                    cell.setCellValue("");
                }
                cell.setCellStyle(cellStyle1);
            }
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 4));

            //第二行
            HSSFRow row2 = sheet.createRow(rowNum++);

            CellStyle cellStyle2 = createTitleCellStyle2(wb);
            for (int j=0;j<5;j++){
                HSSFCell cell= row2.createCell(j);
                if (j==0){
                    cell.setCellValue("字段名");
                }else if(j== 1){
                    cell.setCellValue("类型长度");
                }else if(j==2){
                    cell.setCellValue("是否主键");
                }else if(j==3){
                    cell.setCellValue("是否自增");
                }else{
                    cell.setCellValue("字段描述");
                }
                cell.setCellStyle(cellStyle2);
            }

            CellStyle cellStyle3 = createTitleCellStyle3(wb);
            for (ColumnModel column : table.getListColumn()){
                HSSFRow row3 = sheet.createRow(rowNum++);
                for (int j=0;j<5;j++){
                    HSSFCell cell= row3.createCell(j);
                    if (j==0){
                        cell.setCellValue(column.getColumnName());
                    }else if(j== 1){
                        cell.setCellValue(column.getColumnType());
                    }else if(j==2){
                        cell.setCellValue(column.getColumnKey());
                    }else if(j==3){
                        cell.setCellValue(column.getColumnAuto());
                    }else{
                        cell.setCellValue(column.getColumnComment());
                    }
                    cell.setCellStyle(cellStyle3);
                }
            }
        }
        //写出
        FileOutputStream output = new FileOutputStream(filePath + "\\数据库.xls");
        wb.write(output);
        output.close();

    }

    //标题
    private static CellStyle createTitleCellStyle(HSSFWorkbook wb) {
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();         //创建字体样式
        font.setFontName("黑体");                           //字体
        font.setFontHeightInPoints((short) 14);             //设置字体大小
        cellStyle.setFont(font);                            // 为标题样式设置字体样式
        cellStyle.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());// 设置背景色
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //设置加粗
        getNewCenterStyle(cellStyle);
        return cellStyle;
    }

    //第二行标题
    private static CellStyle createTitleCellStyle2(HSSFWorkbook wb) {
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();         //创建字体样式
        font.setFontName("黑体");                           //字体
        font.setFontHeightInPoints((short) 12);             //设置字体大小
        cellStyle.setFont(font);                            // 为标题样式设置字体样式
        getNewCenterStyle(cellStyle);
        return cellStyle;
    }

    //内容
    private static CellStyle createTitleCellStyle3(HSSFWorkbook wb) {
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();         //创建字体样式
        font.setFontName("黑体");                           //字体
        font.setFontHeightInPoints((short) 10);             //设置字体大小
        cellStyle.setFont(font);                            // 为标题样式设置字体样式
        getNewCenterStyle(cellStyle);
        return cellStyle;
    }

    //设置边框
    private static void getNewCenterStyle(HSSFCellStyle style) {
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    }

}